const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const {
  $Toast
} = require('../../components/base/base');


Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    home: true,
    scrollHeight: app.globalData.windowHeight - app.globalData.navHeight,
    goTop: true,
    all: true,
    first: false,
    second: false,
    three: false,
  },

  scroll: function(e) {
    var that = this;
    var listIndex = (e.detail.scrollTop + this.data.screenHeight) / (this.data.screenWidth * 0.4)
    if (e.detail.scrollTop > app.globalData.navHeight + 40) {
      this.setData({
        goTop: false
      });
      this.setData({
        listIndex: listIndex
      })
    } else {
      this.setData({
        goTop: true
      });
    }

  },

  //返回顶部
  goScrolltop: function(e) {
    var that = this;
    var top = that.data.scrollTop;
    if (top == 1) {
      top = 0;
    } else {
      top = 1;
    }

    that.setData({
      scrollTop: top
    })

  },

  coupon: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: util.api + 'account/coupon.json',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        for (var i = 0; i < res.data.data.length; i++) {
          that.setData({
            coupon1: res.data.data[i].listItem,
            coupon2: res.data.data[i].listItem
          })
        }
        wx.hideLoading()
      },
      fail: function(res) {
        wx.hideLoading()
      }
    })
  },

  all: function() {
    var that = this;
    that.setData({
      all: true,
      first: false,
      second: false
    })
    that.coupon()
  },

  first: function() {
    var that = this;
    that.setData({
      all: false,
      first: true,
      second: false
    })
    that.coupon()
  },

  second: function() {
    var that = this;
    that.setData({
      all: false,
      first: false,
      second: true
    })
  },

  sendMsg: function(e) {
    var that = this;
    var coupons = that.data.coupon1;
    var id = e.currentTarget.dataset.id;
    for (let i=0; i < coupons.length; i++) {
      if (coupons[i].id == id){
        wx.showModal({
          title: '',
          confirmText: "确认",
          confirmColor: "#e93b3d",
          cancelText: "取消",
          cancelColor: "#333",
          content: '是否发送短信通知？',
          success: function (res) {
            if (res.confirm) {
              $Toast({
                content: "商品券短信发送成功",
                icon: 'success',
                duration: 1
              })
            } else if (res.cancel) {

            }
          }
        })
      }
    }
  },

  cancelCoupon: function(e) {
    var that = this;
    var coupons = that.data.coupon1;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    for (let i = 0; i < coupons.length; i++) {
      if (coupons[i].id == id) {
        wx.showModal({
          title: '',
          confirmText: "确认",
          confirmColor: "#e93b3d",
          cancelText: "取消",
          cancelColor: "#333",
          content: '是否取消提货券？',
          success: function (res) {
            if (res.confirm) {
              $Toast({
                content: "商品券已取消",
                icon: 'success',
                duration: 1
              })
              coupons.splice(index, 1)
              that.setData({
                coupon1: coupons
              })
            } else if (res.cancel) {

            }
          }
        })
      }
    }
  },

  verification: function(e){
    var code = e.currentTarget.dataset.code;
    util.navigateTo('../verificationSheet/verificationSheet?code=' + code)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.all()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }

})